/**
 * The options package of the application.
 */
package fr.umlv.m2.android.quizz.options;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.TextView;
import fr.umlv.m2.android.quizz.R;
import fr.umlv.m2.android.quizz.database.Level;
import fr.umlv.m2.android.quizz.database.LevelDb;
import fr.umlv.m2.android.quizz.database.Score;
import fr.umlv.m2.android.quizz.database.ScoreDb;
import fr.umlv.m2.android.quizz.database.Theme;

/**
 * This class defines options activity of the application.
 * 
 * @author Gambouele Bertrand.
 * @author Viravout Alexandre.
 */
public class OptionsActivity extends Activity {
	/**
	 * The sound check box.
	 */
	private CheckBox soundCheckBox;
	/**
	 * The sound level bar.
	 */
	private SeekBar soundLevelBar;
	/**
	 * The sound level value text.
	 */
	private TextView soundLevelValue;
	/**
	 * The sound level button to test the sound level.
	 */
	private Button soundLevelButton;
	/**
	 * The reset scores button.
	 */
	private Button resetScoresButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.options);

		this.soundCheckBox = (CheckBox)findViewById(R.id.optionsSoundCheckBox);
		this.soundLevelBar = (SeekBar)findViewById(R.id.optionsSoundLevelBar);
		this.soundLevelValue = (TextView)findViewById(R.id.optionsSoundLevelValueText);
		this.soundLevelButton = (Button)findViewById(R.id.optionsSoundLevelButton);
		this.resetScoresButton = (Button)findViewById(R.id.optionsResetScoresButton);
		
		this.muteCheckBox(this.soundCheckBox.isChecked());

		this.soundCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				muteCheckBox(isChecked);
			}
		});
		
		final AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
		int maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		int initVolume = (maxVolume / 3) * 2;
		this.soundLevelBar.setMax(maxVolume);
		this.soundLevelBar.setProgress(initVolume);
		this.soundLevelValue.setText(String.valueOf(initVolume));
		
		this.soundLevelBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				soundLevelValue.setText(String.valueOf(progress));
				
				audio.setStreamVolume(
						AudioManager.STREAM_MUSIC,
						progress,
						AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE
				);
			}
		});

		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		final Resources resources = this.getResources();
		alertDialog.setTitle(resources.getString(R.string.optionsSoundLevelDialogTitle));
		alertDialog.setMessage(resources.getString(R.string.optionsSoundLevelDialogText));
		alertDialog.setButton(
				AlertDialog.BUTTON_POSITIVE,
				resources.getString(R.string.optionsSoundLevelDialogButton),
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {}
				}
		);
		
		this.soundLevelButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(audio.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
					MediaPlayer.create(OptionsActivity.this, R.raw.chat).start();
				} else {
					alertDialog.show();
				}
			}
		});
		
		this.resetScoresButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				LayoutInflater factory = LayoutInflater.from(OptionsActivity.this);
				final View resetScoresDialogView = factory.inflate(R.layout.reset_scores_dialog, null);
				
				AlertDialog.Builder builder = new AlertDialog.Builder(OptionsActivity.this);
				builder.setView(resetScoresDialogView);
				builder.setTitle(R.string.scoresResetScoresDialogTitleText);
				
				builder.setPositiveButton(R.string.scoresResetScoresDialogValidateButton, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						final LevelDb levelDb = new LevelDb(OptionsActivity.this);
						final ScoreDb scoreDb = new ScoreDb(OptionsActivity.this);
						levelDb.open();
						scoreDb.open();
						scoreDb.clear();
						
						for(Level level : levelDb.getAllLevels()) {							
							Score score;							
							for(int i=0; i<4; i++) {
								score = new Score(0, 0, 0, level.getId(), 0, Theme.NO_THEME);
								score.setTournamentRank(0);
								scoreDb.insertScore(score);
							}
						}
						
						scoreDb.close();
						levelDb.close();
					}
				});
				
				builder.setNegativeButton(R.string.dialogCancelButton, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {}
				});
				
				builder.show();
			}
		});
	}

	protected void muteCheckBox(boolean isChecked) {
		AudioManager audio = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);

		if(isChecked) {
			audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		} else {
			audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		}
	}
}
